教务处将学生档案、课程详情和选课记录分别存放在不同的表中。请构建一份报告,把 正确的学生姓名与对应的课程对应起来。
你的三项需求
- 查找 Emma Li 的有效课程。
- 列出所有有效选课记录,并附上学生姓名和课程名称。
- 生成 DSAI 学生 PY101 有效选课名单。
准备 PostgreSQL
- 在 pgAdmin 中,打开 TA 文件夹
lab6_7_setup.sql中提供的DSAI2202/lab6_7/并完整运行该文件一次。 - 确认最终记录数: 22 名学生、8 门课程、37 条选课记录对应起来。
- 运行右侧的连接设置。每次建立新的数据库连接时都要重新运行它。
- 在连接这三张表之前,先查看它们的内容。
实验 7 会使用相同的初始化脚本,其中包括 assignments、 submissions, and lms_roster. Today, use the three tables shown here. All people and records are fictional.
✍️ Trace Before You Join
Find enrolment E01. Which student ID and course code does it contain? Use the Data tab to recover the student's name and the course title.
SET search_path TO lab6_7;SET search_path TO lab6_7;
SELECT student_id, student_name, programme
FROM students
ORDER BY student_id;SET search_path TO lab6_7;
SELECT course_code, course_title, credits
FROM courses
ORDER BY course_code;SET search_path TO lab6_7;
SELECT enrolment_id, student_id, course_code,
enrolment_status, score
FROM enrolments
ORDER BY enrolment_id;Small Previews of the Three Tables
students: 22 rows. student_id is the primary key.
| student_id | student_name | programme |
|---|---|---|
| S101 | Alice Wong | DSAI |
| S102 | Ben Lau | CS |
| S105 | Emma Li | CS |
courses: 8 rows. course_code is the primary key.
| course_code | course_title | credits |
|---|---|---|
| SQL101 | SQL Fundamentals | 3 |
| PY101 | Python Programming | 3 |
| WEB101 | Web Databases | 3 |
enrolments: 37 rows, including 30 active and 7 withdrawn. enrolment_id is the primary key.
| enrolment_id | student_id | course_code | enrolment_status |
|---|---|---|---|
| E01 | S101 | SQL101 | active |
| E04 | S104 | SQL101 | withdrawn |
| E06 | S105 | PY101 | active |
Student link
enrolments.student_id → students.student_idA foreign key points to one student.
Course link
enrolments.course_code → courses.course_codeA foreign key points to one course.
Trace E01
S101 + SQL101Alice Wong + SQL Fundamentals.
The repeated IDs connect records; the names and titles stay in their own tables. One student may appear in several enrolments.